﻿using MeterReader.WebApi.Models;
using MeterReader.WebApi.Services.Repositories;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace MeterReader.WebApi.Controllers
{
    [Route("[controller]/[action]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        private readonly IReadRepository _readRepository;
        private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();

        public ValuesController(IReadRepository readRepository)
        {
            _readRepository = readRepository;
        }

        [HttpGet]
        public async Task<TMeter> GetValues(int num) => await _readRepository.ReadDataAsync(num, _cancellationTokenSource.Token);

        [HttpGet]
        public void CancelRead()
        {
            _cancellationTokenSource.Cancel();
            _cancellationTokenSource = new CancellationTokenSource();
        }
    }
}
